#include "IFUException.h"
#include "Sifus.h"
#include "Mask.h"
#include "Linear.h"

#include <stdio.h>
#include <stdlib.h>
#include <iostream>

using namespace std;

Linear::Linear(Sifus *sifus) {
  this->sifus = sifus;
}

Linear::~Linear() {
}

void Linear::doFit() {

  char file[127];
  string maskBasename;
  string maskFilename;
  string maskParFile;
  string maskParFinalFile;

  // obtem a quantidade de mascaras a processar
  int nMaskHor = sifus->conf->getNMaskHor();
  int nMaskVer = sifus->conf->getNMaskVer();
  try {

    for (int i = 0; i < nMaskHor; i++) {
      for (int j = 0; j < nMaskVer; j++) {

	// monta o nome da mascara = maskBasename + maskExt e o nome do arquivo de parametros
	// e o nome do arquivo final de parametros
	sprintf(file, "%s-%02d-%02d", sifus->conf->getMaskBasename().c_str(), j, i);
	maskBasename = file;
	maskFilename = maskBasename + "." + sifus->conf->getMaskExt();
	maskParFile = maskBasename + ".par";
	maskParFinalFile = maskBasename + ".ff";
      
	Mask *mask = new Mask(maskFilename);
	mask->par->add(maskParFile);
	mask->process();
	mask->par->write(maskParFinalFile);

	cout << endl;

	delete mask;

      }
    }

  } catch (IFUException e) {

    e.what();
    exit(1);

  }

}
